<?php

/**
 * Protect against register_globals vulnerabilities.
 * This line must be present before any global variable is referenced.
 */
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
	die( -1 );
}

/**
 * SMW_PropertiesParser Class
 */

class SMW_PropertiesParser{

    const MANDATORY_PROPS_KEY = 'MustHaveProperty';
    const OPTIONAL_PROPS_KEY = 'OptionalProperty';
    
    /**
    * Querys the SMW about the mandatory properties of the category
    * 
    * @param $category_content Object Result of the SMW parser
    * @return Array Array with the mandatory properties as key or an empty array
    */
    public static function getMandatoryProperties($category_content){
         $mandatory_props_key = SMWPropertyValue::makeProperty( self::MANDATORY_PROPS_KEY );
         $props = array_keys($category_content->mSMWData->getPropertyValues($mandatory_props_key));
         if( count($props) > 0){
             return array_flip($props);
         }else{
             return array();
         }
    }
    
    /**
    * Format the error box containing the missing properties for on given category
    *
    * @param $missing_property Array Array of missing properties
    * @return String HTML code of the error box
    */
    
    public static function printErrorBox($missing_property){
        $error = '';
        foreach($missing_property as $key => $props){
               $t_c = Title::newFromText($key);
               $link_c = $t_c->getFullURL();
               //TODO: internationalize this
               $error .= '<div class="SMWP_properties_error_list"><span class="error_category">Missing propert'.(count($props)>1? 'ies' : 'y').' for ['.$link_c.' '.$key.']:</span><ul>';
               foreach($props as $porp => $prop_name){
                   $t = Title::newFromText("Property:".$prop_name);
                   $link = $t->getFullURL();
                   $error .= '<li><span class="error_property">['.$link.' '.$prop_name.']</span></li>';
               }
               $error .= '</ul></div>';
        }
        return $error;
    }
    
}